//
// Created by yangy on 2018/6/28.
//

#ifndef SUPERAUDIOPLAYER_WIAUDIO_H
#define SUPERAUDIOPLAYER_WIAUDIO_H

#include "include/ffmpeg.h"
#include "WIQueue.h"
#include "androidLog.h"
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include "JavaCallBack.h"
#include "soundTouch.h"

#define LEFT_CHANNEL 1
#define RIGHT_CHANNEL 0
using namespace soundtouch;

//音频解码所需信息封装
class WIAudio {
public:

    //播放时长
    double duration = 0;
    //当前播放时间
    double new_time = 0;

    double clock = 0;
    //音量
    int volumePercent;
    //时间单位
    AVRational time_base;

    //音頻流索引浩
    int audio_stream_index;
    //音頻流信息
    AVCodecParameters *av_codec_par = NULL;
    //音频解码器上下文
    AVCodecContext *av_codec_ctx = NULL;
    //音频解码队列
    WIQueue *queueAudio = NULL;
    //播放状态
    WIPlayState *playState;

    JavaCallBack *callBack = NULL;

    //播放线程
    pthread_t thread_play;
    //压缩数据
    AVPacket *avPacket = NULL;

    int ret = -1;

    AVFrame *avFrame;

    //重采样上下文
    SwrContext *swrContext = NULL;

    //pcm缓冲区
    uint8_t *buffer;
    //soundTounch 处理过的音频缓冲区
    uint8_t *sound_buffer;

    int data_size = 0;



    //引擎对象
    SLObjectItf enginObject = NULL;
    //引擎接口
    SLEngineItf engineItf = NULL;

    //混音器对象(混音器作用是做声音处理)
    SLObjectItf outputmixObject = NULL;
    //混音器环境接口
    SLEnvironmentalReverbItf outputEnvironmentalReverbItf = NULL;
    const SLEnvironmentalReverbSettings reverbSettings = SL_I3DL2_ENVIRONMENT_PRESET_STONECORRIDOR;

    //播放器对象
    SLObjectItf pcmPlayerObject = NULL;
    //播放接口
    SLPlayItf playItf = NULL;
    //音量控制
    SLVolumeItf pcmVulumeItf = NULL;
    //声道接口
    SLMuteSoloItf pcmMuteSoloItf = NULL;
    //播放队列
    SLAndroidSimpleBufferQueueItf simpleBufferQueueItf = NULL;

    int sample_rate;

    //soundTouch
    SoundTouch *soundTouch = NULL;
    SAMPLETYPE *sample_buffer = NULL;
    bool isFinished = false;
    uint8_t *out_buffer;
    int nb;
    int num;

    bool record;

public:
    void play();

    int resampleAudio(void **buffer);

    WIAudio(WIPlayState *playState, int sample_rate, JavaCallBack *callBack);

    ~WIAudio();

    void initopensl();

    int getresampleRate(int format);


    void pause();

    void resum();

    void stop();

    void releas();

    void setVolume(int percent);

    void setMute(int mute);

    int getsoundTouchDate();

    void setpitch(double pitch);

    void settempo(double tempo);

};


#endif //SUPERAUDIOPLAYER_WIAUDIO_H
